home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / fragment tool / utilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.5 KB  |  215 lines

  1. /*
  2.     File:        Utilities.c
  3.  
  4.     Contains:    General utility routines
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #ifndef __TYPES__
  26.     #include <Types.h>
  27. #endif
  28.  
  29. #ifndef __TEXTUTILS__
  30.     #include <TextUtils.h>
  31. #endif
  32.  
  33. #ifndef __DIALOGS__
  34.     #include <Dialogs.h>
  35. #endif
  36.  
  37.  
  38. #ifndef __CODEFRAGMENTS__
  39.     #include <CodeFragments.h>
  40. #endif
  41.  
  42. #include <Folders.h>
  43. #include <Resources.h>
  44.  
  45.  
  46.  
  47.  
  48.  
  49. #include "FragmentTool.h"
  50. #include "Utilities.h"
  51.  
  52. #include "ProtoTypes.h"
  53.  
  54.  
  55.  
  56. //
  57. // Tell the user that something went wrong
  58. //
  59. void AlertUser ( short messageCode, short errorNum, StringPtr theString )
  60. {
  61.     Str255        messageString;
  62.     Str255        numberString;
  63.     
  64.     if ( messageCode > 0 )
  65.         GetIndString ( messageString, rErrorStrings, messageCode );
  66.     else
  67.         messageString[0] = '\0';
  68.      
  69.     if ( errorNum != 0 )
  70.         NumToString ( errorNum, numberString );
  71.     else
  72.         numberString[0] = '\0';
  73.      
  74.     ParamText ( messageString, numberString, theString, (StringPtr) "\p" );
  75.     
  76.     StopAlert ( kErrorDialog, nil );
  77.     
  78.     // We need to clear the param text so it isn't used by mistake
  79.     ParamText ( (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p" );
  80.     
  81.     return;
  82. }
  83.  
  84.  
  85.  
  86. pascal void OutlineUserItem ( DialogRef theDialog, short theItem )
  87. {
  88.     short    theType;
  89.     Handle    theHan;
  90.     Rect    theRect;
  91.     
  92.     GetDialogItem ( theDialog, theItem, &theType, &theHan, &theRect );
  93.     PenNormal ( );
  94.     InsetRect ( &theRect, -1, -1 );
  95.     FrameRect ( &theRect );
  96.     
  97.     return;
  98. }
  99.  
  100.  
  101.  
  102. StringPtr CopyPStr ( Str255    inSourceStr, StringPtr outDestStr, int16 inDestSize )
  103. {
  104.     int8 dataLen = inSourceStr[0] + 1;
  105.     if ( dataLen > inDestSize )
  106.         dataLen = inDestSize;
  107.     
  108.     BlockMoveData ( inSourceStr, outDestStr, dataLen );
  109.     outDestStr[0] = dataLen - 1;
  110.     return outDestStr;
  111. }
  112.  
  113.  
  114.  
  115. StringPtr ConcatPStr ( Str255 ioFirstStr, Str255 inSecondStr, int16 inDestSize )
  116. {
  117.     int8 charsToCopy = inSecondStr[0];
  118.     
  119.     if ( ioFirstStr[0] + charsToCopy > inDestSize - 1 )
  120.         charsToCopy = inDestSize - 1 - ioFirstStr[0];
  121.  
  122.     BlockMoveData ( inSecondStr + 1,  ioFirstStr + ioFirstStr[0] + 1, charsToCopy );
  123.     ioFirstStr[0] += charsToCopy;
  124.     
  125.     return ioFirstStr;
  126. }
  127.  
  128.  
  129.  
  130. StringPtr OSTypeToPStr ( OSType inOSType, StringPtr outString )
  131. {
  132.     BlockMoveData ( &inOSType, outString + 1, sizeof ( OSType ) );
  133.     outString[0] = sizeof ( OSType );
  134.  
  135.     return outString;
  136. }
  137.  
  138.  
  139.  
  140. void BlockClear ( Ptr ptr, char value, Size size )
  141. {
  142.     int i;
  143.     
  144.     for ( i = 0; i < size; i++ )
  145.         *ptr++ = value;
  146.     
  147.     return;
  148. }
  149.  
  150.  
  151.  
  152. Boolean IsAResource ( Handle theHan )
  153. {
  154.     const short kResNotFound = -1;
  155.     return HomeResFile ( theHan ) != kResNotFound;
  156. }
  157.  
  158.  
  159.  
  160. OSErr CreateTemporaryFile ( FSSpecPtr theSpec )
  161. {
  162.     OSErr        theErr = noErr;
  163.     int16        theVol;
  164.     int32        theDir;
  165.     Str255        tmpStr = "\p";
  166.     Str255        theName = "\pFragmentTool ";
  167.     
  168.     
  169.     
  170.     theErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &theVol, &theDir );
  171.     if ( theErr ) goto CleanupAndBail;
  172.     
  173.     OSTypeToPStr ( TickCount ( ), tmpStr );
  174.     ConcatPStr ( theName, tmpStr, sizeof ( Str255 ) );
  175.     theErr = FSMakeFSSpec ( theVol, theDir, theName, theSpec );
  176.     if ( theErr != fnfErr )
  177.     {
  178.         theErr = (theErr == fnfErr) ? dupFNErr : theErr;
  179.         goto CleanupAndBail;
  180.     }
  181.     
  182.     theErr = FSpCreate ( theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  183.     if ( theErr ) goto CleanupAndBail;
  184.     
  185.     // The file has been created with both forks, but it doesn't have a
  186.     // resource map yet. If we try to open it now, we'll get an eofErr (-39).
  187.     FSpCreateResFile ( theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  188.     
  189.     
  190.     // goto's are okay for error handling
  191. CleanupAndBail:
  192.     return theErr;
  193. }
  194.  
  195.  
  196.  
  197. #if DEBUGGING
  198. void DebugStrNum ( Str255 str, long num )
  199. {
  200.     Str255 debug_str, tmp_str;
  201.     
  202.     BlockMoveData ( &str[0], &debug_str[0], str[0] + 1 );
  203.     
  204.     NumToString ( num , &tmp_str[0] );
  205.     debug_str[debug_str[0] + 1] = ' ';
  206.     BlockMoveData ( &tmp_str[1], &debug_str[debug_str[0] + 2], tmp_str[0] );
  207.     debug_str[0] = debug_str[0] + tmp_str[0] + 1;
  208.     DebugStr ( debug_str );
  209.     
  210.     return;
  211. }
  212. #endif
  213.  
  214.  
  215.